import config from '../utils/config'

export const formatTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('-') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

export const formatDate = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return (
    [year, month, day].map(formatNumber).join('-')
  )
}

const formatNumber = (n) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

// 获取用户信息
let isShowModal = false
export const getUserInfo = (isShowLogin = true) => {
  return new Promise((resolve, reject) => {
    const accountInfo = wx.getAccountInfoSync();
    const { appId } = accountInfo.miniProgram
    wx.setStorageSync('appId', appId)
    const openId = wx.getStorageSync('openId')
    const phoneNumber = wx.getStorageSync('phoneNumber')
    if (openId && phoneNumber) {
      resolve(true)
      return
    } else {
      wx.login({
        success: res => {
          wx.request({
            url: `${config.host}/wx/auth/login`,
            method: 'POST',
            data: { appId, code: res.code },
            success: (res) => {
              const { openid, token } = res.data.data
              wx.setStorageSync('openId', openid)
              wx.setStorageSync('token', token)
              wx.request({
                url: `${config.host}/wx/auth/info?openId=${openid}`,
                header: {
                  Authorization: `Bearer ${token}`
                },
                success: (res) => {
                  console.log(res)
                  if (res.data.data) {
                    const { avatarUrl, nickName, phoneNumber } = res.data.data
                    wx.setStorageSync('nickName', nickName)
                    wx.setStorageSync('avatarUrl', avatarUrl)
                    wx.setStorageSync('phoneNumber', phoneNumber)
                    resolve(res)
                  } else {
                    if (isShowLogin) {
                      if (!isShowModal) {
                        isShowModal = true
                        jumpToLogin()
                      }
                      reject(res)
                    }
                  }
                }
              })
            }
          })
        }
      })
    }
  })
}




export const jumpToLogin = () => {
  wx.showModal({
    title: '提示',
    content: '请前往授权登录',
    success: (res) => {
      if (res.confirm) {
        wx.reLaunch({
          url: '/pages/login/login'
        })
      }
    },
    complete: () => {
      isShowModal = false
    }
  })
}

// 获取屏幕的像素密度
var scale = wx.getSystemInfoSync().screenWidth / 750;
// 将px转换为rpx
export const pxToRpx = (px) => {
  return px * scale;
}